package com.hry.igoods.controller.backend;

import com.hry.igoods.common.Const;
import com.hry.igoods.common.ResponseCode;
import com.hry.igoods.common.ServerResponse;
import com.hry.igoods.pojo.User;
import com.hry.igoods.service.CategoryService;
import com.hry.igoods.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 *
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加商品分类
     *
     * @param session
     * @param categoryName
     * @param parentId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add_category.do", method = RequestMethod.GET)
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {
        // 判断用户是否存在
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "管理员用户需要登录");
        }
        // 判断用户是不是管理员
        if (userService.checkAdminRole(user).isSuccess()) {
            // 此用户为管理员
            return categoryService.addCategory(categoryName, parentId);

        }
        // 非法用户
        return ServerResponse.createByErrorMessage("此用户没有权限访问");
    }

    /**
     * 更新分类名称
     *
     * @param session
     * @param categoryName
     * @param categoryId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update_category_name.do", method = RequestMethod.GET)
    public ServerResponse updateCategoryName(HttpSession session, String categoryName, Integer categoryId) {
        // 判断用户是否为空
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(Const.Role.ROLE_ADMIN, "管理员用户需要登录");
        }
        // 判断是否是管理员
        if (userService.checkAdminRole(user).isSuccess()) {
            return categoryService.updateCategoryName(categoryName, categoryId);
        }
        return ServerResponse.createByErrorMessage("无权限操作");
    }

    /**
     * 查询子节点和子节点的平行节点
     *
     * @param session
     * @param categoryId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "get_children_parallel_category.do", method = RequestMethod.GET)
    public ServerResponse getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        // 判断当前用户是否为空
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(Const.Role.ROLE_ADMIN, "管理员需要登录");
        }
        // 判断是否是管理员
        if (userService.checkAdminRole(user).isSuccess()) {
            return categoryService.getChildrenParallelCategory(categoryId);
        }
        return ServerResponse.createByErrorMessage("该用户没有权限操作");
    }

    /**
     * 获取当前categoryId并递归查询子类categoryId
     */
    @ResponseBody
    @RequestMapping(value = "get_category_and_children_category.do", method = RequestMethod.GET)
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        // 判断当前用户是否为空
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(Const.Role.ROLE_ADMIN, "管理员需要登录");
        }
        // 判断是否是管理员
        if (userService.checkAdminRole(user).isSuccess()) {
            return categoryService.getCategoryAndDeepChildrenCategory(categoryId);
        }
        return ServerResponse.createByErrorMessage("该用户没有权限操作");

    }
}
